/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Nov 14, 2013  3:00:23 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.interfaces
 * File Name : PropertyUnitController.java
 * TODO
 */
package com.tis.interfaces;

import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.entitys.synchro.staging.AGenDesc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import promis.utility.CommonService;
import apps.ApplConfiguration;
import apps.business.process.commons.propertyunit.PropertyUnitProcessing;
import com.tis.interfaces.commons.ServletCommon;
import com.tis.interfaces.commons.XmlResultback;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 14, 2013 : 3:00:23 PM 
 * CreateBy=Chatree Siangklom
 * 
 * PropertyUnitController
 * TODO
 */
@Controller
@RequestMapping(value=PropertyUnitController.url)
public class PropertyUnitController extends ServletCommon {

	private static final Logger logging = LoggerFactory.getLogger(PropertyUnitController.class);	
	public static final String url = "/servlet/unit";
	
	@RequestMapping(method = RequestMethod.GET)
	public HttpEntity<byte[]> index(HttpServletRequest request, HttpServletResponse response) {
		logging.info("Welcome PropertyUnitController--->index");
		XmlResultback xmlResultback = new XmlResultback(promis.utility.ProcessModule.PROPERTY_UNIT.toString(), null, new Date());
		
		try {
			
			PropertyUnitProcessing PropertyUnitProc = new PropertyUnitProcessing();
			Map mapValue = super.initParam(request);

			AGenDesc aGenDesc = new ApplConfiguration().GetInterfaceChannel(
																				CommonService.ConvertStringToBigDecimal("1001"), 
																				CommonService.ConvertStringToBigDecimal("1002"), 
																				promis.utility.BusinessModule.COMMONS.toString(), 
																				promis.utility.ProcessModule.PROPERTY_UNIT.toString());
			if(CommonService.IsNullRString(aGenDesc.getValue3()).equals("Q")){
				new apps.promis.out.common.SyncQueueAction().PutQueue(	promis.utility.BusinessModule.COMMONS.toString(), 
																		promis.utility.ProcessModule.PROPERTY_UNIT.toString(),
																		null,
																		processAction,
																		mapValue);
				xmlResultback.setMsgcode("QS");
				xmlResultback.setMsgDescription("Queue Success.");
			}else if(CommonService.IsNullRString(aGenDesc.getValue3()).equals("D")){
				if(action.toUpperCase().equals(promis.utility.InterfaceAction.PropertyUnit.PER_BUILDING.toString())){
					
					PropertyUnitProc.CreatedToStagingByBuilding(company, project, buildingPhase);
					xmlResultback.setMsgcode("PS");
					xmlResultback.setMsgDescription("Processing Success.");
				} else if(action.toUpperCase().equals(promis.utility.InterfaceAction.PropertyUnit.PER_FLOORZONE.toString())){
					PropertyUnitProc.CreatedToStagingByFloorZone(company, project, buildingPhase, zoneCode);
					xmlResultback.setMsgcode("PS");
					xmlResultback.setMsgDescription("Processing Success.");
				} else if(action.toUpperCase().equals(promis.utility.InterfaceAction.PropertyUnit.PER_UNIT.toString())){
					PropertyUnitProc.SingleUnit(company, project, buildingPhase, zoneCode, unitNumber);
					xmlResultback.setMsgcode("PS");
					xmlResultback.setMsgDescription("Processing Success.");
				}
			}
	    } catch (Exception e) {
	    	xmlResultback.setMsgcode("EE");
			xmlResultback.setMsgDescription(e.getMessage());
			
	    	e.printStackTrace();
	    	logging.error("Error:" + e.getMessage());
		}
		
		return(ToXml(xmlResultback));
	}
}
